<?php
include_once('../lib/config.php');
require ($war_root.'/lib/verify.php');
require ($war_root.'/lib/has_char.php');
require ($war_root.'/lib/is_approved.php');
require ($war_root.'/lib/is_admin.php');


$_category = "Admin";
$_title = "";
$admin_active = 1;
$log_char = $character["character_name"];
$debug=0;

if (@$_GET ['a'] != "adding" || @$_GET ['s'] == 2) require ($war_root.'/lib/head.php');

$page = @$_GET ['p'];
$action = @$_GET ['a'];
$raid_id = @$_GET ['raid'];
$system_id = @$system ["system_id"];


/*****************************
* Start Raid part
*****************************/

$_category = "Raid";
$_title = "View Raids";
boxTop ( "$_category: $_title" );

//
//Select what to add
//
if ($action == "add") {
	echo '<b>Add a raid</b><br /><br />' . '<form action="'.$war_http.'/admin/raid.php?a=adding" method="post">' . '<input type="hidden" value="' . $system ["system_id"] . '" name="system_id">' . '<i>Instance</i><br/>' . '<select name="instance_id">';

	$sql = "select * from wm_instances where instance_tier = " . $system ["system_tier"];
	$result = $db->query ( $sql );
	$i = 0;
	while ( $inst = $db->fetch_assoc ( $result ) ) {
		$i ++;
		$chk = "";
		if ($i == 1) {
			$chk = " selected";
		}
		echo '<option value="' . $inst ["instance_id"] . '"' . $chk . '>' . $inst ["instance_name"];

	}
	echo '</select>' . '<br /><br />' . '<i>Date</i><br/>';
	echo date_select ();
	echo '<br /><br />' . '<input type="submit" name="submit" value="Add Raid"><br />' . '</form>';


}
//
//Adding the raid to the database
//
elseif ($action == "adding") {

	$raid_system_id = $_POST ["system_id"];
	$raid_instance = $_POST ["instance_id"];
	$month = $_POST ["month"] + 1;
	$day = $_POST ["day"];
	$year = $_POST ["year"];
	$date_string = "$month/$day/$year 20:00:00";
	$raid_time = strtotime ( $date_string );
	$sql = "insert into wm_raids (raid_instance, raid_date, raid_system_id) values ('$raid_instance', FROM_UNIXTIME('$raid_time'), '$raid_system_id')";

	$result = $db->query ( $sql );

	$ii = $db->insert_id ( $result );

	reprocessDKP ( $db, $system ["system_id"] );

	header ( "Location: $war_http/admin/loot.php?s=1&instance=$raid_instance&raid=$ii" );

}
//
//Modify a already existing raid
//
elseif ($action == "modify") {

	$raid_id = $_POST["raid_id"];
	$raid_system_id = $_POST ["system_id"];
	$raid_instance = $_POST ["instance_id"];
	$month = $_POST ["month"] + 1;
	$day = $_POST ["day"];
	$year = $_POST ["year"];
	$date_string = "$month/$day/$year 20:00:00";
	$raid_time = strtotime ( $date_string );

	//$sql = "insert into wm_raids (raid_instance, raid_date, raid_system_id) values ('$raid_instance', FROM_UNIXTIME('$raid_time'), '$raid_system_id')";
	$sql = "UPDATE wm_raids SET raid_instance='$raid_instance', raid_date='$raid_time' WHERE raid_id='$raid_id'";
	$result = $db->query ( $sql );

	$ii = $db->insert_id ( $result );

	reprocessDKP ( $db, $system ["system_id"] );

	header ( "Location: $war_http/admin/loot.php?s=1&instance=$raid_instance&raid=$ii" );

}
//
//Edit a raid (WIP)
//
elseif ($action == "edit") {
	$foo="bar";
}
//
//Delete a raid (WIP)
//
elseif ($action == "delete") {
	$raid_id = $_GET ['raid'];
	$system_id = $system ["system_id"];
	$boss_instance = @$_GET ['instance'];
	if ($_GET ['sure'] == "yes") {
		//Delete from wm_raids
		$sql = "delete from wm_raids where raid_id = $raid_id and raid_system_id = $system_id";
		$result = $db->query ( $sql );

		if ($result == 1) echo "Successfully deleted from wm_raids<br \>";
		else {
			echo "Failed to delete from wm_raids, please contact an administrator!";
			$delf = true;
			sql_debug($db->error());
		}

		//Delete from wm_attendees and wm_loots
		$sqlf = "select * from wm_credits where credit_raid = $raid_id and credit_system_id = $system_id";
		$resultf = $db->query ( $sqlf );
		while ( $cred = $db->fetch_assoc ( $resultf ) ) {
			//$credit_id[] = $cred['credit_id'];
			//actual deletion of attendees
			$sql = "delete from wm_attendees where attendee_credit = " . $cred ['credit_id'] . " and attendee_system_id = $system_id";
			$result = $db->query ( $sql );
			if (! $result) {
				echo "Failed to delete from wm_attendee, please contact an administrator!";
				$delf = true;
				sql_debug($db->error());
			}

			//actual deletion of loot
			$sqll = "delete from wm_loots where loot_credit = " . $cred ['credit_id'] . " and loot_system_id = $system_id";
			$resultl = $db->query ( $sqll );
			if (! $resultl) {
				echo "Failed to delete from wm_loots, please contact an administrator!";
				$delf = true;
				sql_debug($db->error());
			}
			unset ( $result, $resultl );
		}

		//Delete from wm_credits
		$sql = "delete from wm_credits where credit_system_id = $system_id and credit_raid = $raid_id";
		$result = $db->query ( $sql );

		if ($result == 1) echo "Successfully deleted from wm_credits<br \>";
		else{
			echo "Failed to delete from wm_credits, please contact an administrator!";
			sql_debug($db->error());
		}

		reprocessDKP ( $db, $system ["system_id"] );

		if ($delf === true) echo '<br \><br \>There was some errors deleting raid T' . $system ["system_tier"] . 'R' . $raid_id . '.';
		else echo '<br \><br \>Raid T' . $system ["system_tier"] . 'R' . $raid_id . ' deleted successfully.';
	}
	else {

		echo 'Are you sure you want to delete raid: T' . $system ["system_tier"] . 'R' . $raid_id . '?<br \>' . 'This will delete all attendees and loot gained in this raid(will affect overal DKP!).<br \><i>Might take a while</i><br \>';
		echo '<form action="'.$war_http.'/admin/raid.php" method="get">' . '<input type="hidden" value="delete" name="a">' . '<input type="hidden" value="' . $raid_id . '" name="raid">' . '<input type="hidden" value="' . $system ["system_id"] . '" name="system_id">' . '<input type="hidden" value="' . $raid_id . '" name="raid">' . '<input type="hidden" value="' . $boss_instance . '" name="instance">' . '<input type="hidden" value="yes" name="sure">';
		echo '<br />';
		echo '<input type="submit" value="Yes">  <input type="button" value="No" onClick="javascript:window.history.back()"></form>';

	}

}
else //
//Show raids and add/edit/delete links
//
{
	$system_id = $system ["system_id"];
	if(isset($_GET['nr'])) $n_raids = $_GET['nr'];
	else $n_raids = 1;






	echo 'Raids that have been registered on this tier:<br>' . '<i><a href="'.$war_http.'/admin/raid.php?a=add">add a raid</a>(Select the middle button to modify individual raids).</i><br /><br />' . '<table>' . '<tr>' . '<th width="50">Modify</th>' . '<th>Where</th>' . '<th>When</th>' . '<th>Bosses</th>' . '<th width="40">ID</th>' . '</tr>';

	$per_page = 20;
	$offset = ($n_raids -1) * $per_page;
	$sql = "select *, wm_instances.instance_name
                        from wm_raids left join wm_instances on wm_raids.raid_instance = wm_instances.instance_id
                        where wm_raids.raid_system_id = $system_id order by raid_date desc limit $offset, $per_page";
	$result = $db->query ( $sql );

	$i = 0;

	while ( $raid = $db->fetch_assoc ( $result ) ) {
		$i ++;
		$sqlb = "select *, wm_bosses.* from wm_credits left join wm_bosses on wm_bosses.boss_id = credit_boss where credit_raid = '" . $raid ["raid_id"] . "' and credit_system_id = $system_id order by credit_date asc";
		$resb = $db->query ( $sqlb );
		$ib = 0;
		//$bosses = array();
		while ( $cred = $db->fetch_assoc ( $resb ) ) {
			$ib ++;
			$bosses [] = $cred ['boss_name'] . '| <i>(' . date ( "H:i", strtotime ( $cred ["credit_date"] ) ) . ')</i><br />';
		}

		echo '<tr>';
		echo '<td align="center">';
		// List raid details
		echo '<a href="'.$war_http.'raids.php?a=view&id=' . $raid ["raid_id"] . '"><img src="' . $war_http . '/images/list.gif" alt="View"></a> ';
		// Wat. Looks like edit
		echo '<a href="'.$war_http.'/admin/loot.php?s=3&raid='.$raid["raid_id"].'"><img src="' . $war_http . '/images/edit2.gif" alt="Modify"></a> ';
		echo '<a href="'.$war_http.'/admin/raid.php?a=delete&raid=' . $raid ["raid_id"] . '"><img src="' . $war_http . '/images/cross.gif" width="13" height="15" alt="Delete"></a> ';
		echo '</td>';
		echo '<td>' . $raid ["instance_name"] . '</td>';
		echo '<td>' . date ( "D d M Y", strtotime ( $raid ["raid_date"] ) ) . '</td>';
		echo '<td>Defeated: ' . $ib;
		echo ' <a href="javascript:unhide(\'hide' . $i . '\');">Show/hide</a><br \><div id="hide' . $i . '"  class="hidden">';
		if ($bosses != true) {
			echo "No bosses killed.";
		}
		else {
			foreach ( $bosses as $bos ) {
				list ( $boss, $time ) = explode ( '|', $bos );
				echo $boss . $time;
			}
		}
		echo '</div></td>';
		echo '<td>T' . $system ["system_tier"] . 'R' . $raid ["raid_id"] . '</td>';
		echo '</tr>';
		unset ( $bosses );
	}
	echo '</table>';

	$query = "SELECT * FROM wm_raids WHERE raid_system_id = $system_id";
	$result = $db->query($query);
	$numr = $db->num_rows($result);


	$maxPage = ceil($numr/$per_page);

	$self = $_SERVER['PHP_SELF'];
	$nav  = '';

	for($page = 1; $page <= $maxPage; $page++) {
		if ($page == $n_raids) $nav .= " $page ";
		else $nav .= " <a href=\"$self?p=raid&nr=$page\">$page</a> ";
	}



	if ($n_raids > 1){
		$page  = $n_raids - 1;
		$prev  = " <a href=\"$self?p=raid&nr=$page\">[Prev]</a> ";
	}
	else $prev  = ' ';

	if ($n_raids < $maxPage){
		$page = $n_raids + 1;
		$next = " <a href=\"$self?p=raid&nr=$page\">[Next]</a> ";
	}
	else $next = ' ';
	echo "<div align=center>$prev $nav $next</div>";




}
boxBot ();

require ($war_root.'/lib/foot.php');


?>